<template>
  <a-modal :open="open" width="500px" title="套餐维护" centered :ok-button-props="{ disabled: !isCompleted }" @ok="onOk" @cancel="onCancel">
    <a-form class="my-10" :model="formState">
      <a-form-item name="packageName" label="名称" class="w-3/4" v-bind="validateInfos.packageName">
        <a-input v-model:value="formState.packageName" placeholder="请输入" />
      </a-form-item>
      <a-form-item name="outorder" label="顺序号" class="w-3/4">
        <a-input v-model:value="formState.outorder" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const formState = ref({})
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const emit = defineEmits(['update:open', 'success'])

const rulesState = ref({
  packageName: [{ required: true, message: '请输入名称' }]
})
const { isCompleted, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  const { code, msg } = await post('/lis/LisPackage/doSaveLisPackage', formState.value)
  code !== 0 ? message.error(msg || '操作失败') : message.success('保存成功')
  emit('update:open', false)
  emit('success')
}

const onCancel = () => {
  emit('update:open', false)
}
watch(
  () => props.open,
  () => {
    formState.value = { ...props.rowData }
  },
  {
    immediate: true
  }
)
</script>
